#include"DemoDestoryFixture.h"
#include"LSYEasyWorld.h"
#include"LSYEasyBody.h"
#include"Box2D\Box2D.h"
#include"GLES-Render.h"
#include<cmath>
#include"Tool.h"

USING_NS_CC;

bool DemoDestoryFixture::init()
{
	if (!AbstractBox2DTest::init(b2Vec2(0, -10)))
	{
		return false;
	}
	schedule(schedule_selector(DemoDestoryFixture::work), 3, CC_REPEAT_FOREVER, 3);
	return true;
}

void DemoDestoryFixture::box2DAppReady()
{
	pDebug->AppendFlags(b2Draw::e_centerOfMassBit);
	auto size = Director::getInstance()->getWinSize();
	LSYEasyBody::createRectangle(b2Vec2(0, 0), size.width, size.height);

	b = LSYEasyBody::createBox(b2Vec2(250, 200), b2Vec2(30, 30));
	LSYEasyBody::fixBodyAt(b, b2Vec2(250, 200));
	b->SetAngularVelocity(PI / 3);

	addFixture();
}

void DemoDestoryFixture::addFixture()
{
	float radius = 80;
	float angle = 2*PI / 8;
	b2CircleShape* circle = new b2CircleShape;
	b2Vec2 localPosition;
	
	for (int i = 0; i < 8; ++i)
	{
		circle->m_radius = 30 * PTM_RADIO;
		localPosition.x = sinf(angle*i)*radius*PTM_RADIO;
		localPosition.y = cosf(angle*i)*radius*PTM_RADIO;
		circle->m_p = localPosition;
		b->CreateFixture(circle, 1);
	}

	REALSE_NULL(circle);
}

void DemoDestoryFixture::work(float dt)
{
	if (b->GetFixtureList())
	{
		b->DestroyFixture(b->GetFixtureList());
	}
}
